package com.yycx.module.bus.provider.handler.haiying.settle.calculate;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 未核算单列表
 *
 * @author zyf
 */
@Component
public class HyCalculateOrderTab1ListHandler implements PageInterceptor<HyOrder> {


    @Autowired
    private TaskService taskService;


    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery<HyOrder> cq, PageParams pageParams, EntityMap params) {
        OpenUser openUser = OpenHelper.getUser();
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String orderNo = cq.getParams("orderNo");
        cq.eq(HyOrder.class, "deptId");
        cq.eq(HyOrder.class, "orderType");
        cq.eq(HyOrder.class, "saleManName");
        cq.like(BusCustom.class, "customName");
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge("sendDate", beginDate);
            cq.le("sendDate", endDate);
        }
        String userName = openUser.getUsername();
        //非管理员
        if (!userName.contains("admin")) {
            //cq.eq("ho.userId", openUser.getUserId());
            cq.eq("ho.companyId", openUser.getCompanyId());
        } else {
            cq.eq("ho.companyId", openUser.getCompanyId());
        }
        cq.eq(BusDept.class, "deptType");
        //已审批
        cq.eq("orderState", 2);
        //已发货
        cq.eq("sendState", 1);
        //TODO 部门审核状态
        //未申请核算
        cq.eq("calculateState", 0);
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("applyIncId");
            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }

}
